<?php
namespace frontend\controllers;

use Yii;
use yii\web\Controller;

use yii\helpers\Html;

use common\helpers\Test;
use frontend\models\Articles;

use yii\data\Pagination;

/**
* 文章控制器
* @author:liyy-k
*/
class ArticleController extends Controller
{
    //设置布局文件
    public $layout = 'base';
    public $pageSize = 5;
    
    //设置独立控制器
    public function actions()
    {
        return [
            // 用类来申明操作成功
            //'test' => 'frontend\components\HelloAction',
    
            // 用配置数组申明 "view" 动作
//             'view' => [
//                 'class' => 'yii\web\ViewAction',
//                 'viewPrefix' => '',
//             ],
        ];
    }
    
    //过滤器的作用，有前过滤和后过滤
    public function behaviors()
    {
        return [
            [
                //确实执行了
                'class' => 'frontend\components\ActionTimeFilter',
                'only' => ['index', 'view'],  //过滤器只是用在那些操作上
                'except'=>[],//过滤器不使用在那些操作上
                /* 'lastModified' => function ($action, $params) {
                    $q = new \yii\db\Query();
                    return $q->from('user')->max('updated_at');
                }, */
                ],
                ];
    }

    public function actionIndex()
    {
        $query = Articles::find()->where(['is_public' => 1]);
        $count = $query->count();

        $pagination = new Pagination(['totalCount' => $count]);
        $pagination->pageSize = $this->pageSize;
        $list = $query->offset($pagination->offset)
        ->limit($pagination->limit)
        ->orderBy('id')
        ->with(['category','tags'])
        ->all();
        
        return $this->render('index',array('list'=>$list,'pager'=>$pagination));
    }
    
    /**
    * 查看文章详情
    * @param:unknowtype
    * @return:
    */
    public function actionView($id)
    {

        $info = Articles::find()
        ->where('is_public = 1 and id =:id')
        ->addParams([':id' => $id])
        ->orderBy('id')
        ->with(['category','tags'])
        ->one();

        return $this->render('view',array('info'=>$info));
    }
    
    
    
    

    //重新设置视图的目录
    //     public function getViewPath()
    //     {
    //         return  '@app/view2';
    //     }
    
      
    
    
    
    
    
    
    
    
}
